const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");
let fs = require("fs")
const CLIPDATA_TYPE = {
    max:{
        fixedhp: 298.75,
        fixedAtt: 19.45,
        fixdefence: 23.15,
        reaction : 23.31,
        attRate: 5.83, 
        hpRate: 5.83, 
        criticalRate: 3.89,
        criticalStrike: 7.77,
        defRate: 7.29,
        charging: 6.48
    },
    mid:{
        fixedhp: 253.94,   // 209.13  239   268.88   298.75
        fixdefence: 19.675, // 16.2   18.52  20.83  23.15
        fixedAtt: 16.535,   //  13.62   15.56   17.51  19.45
        reaction : 19.815,  // 16.32   18.65  20.98  23.31
        attRate: 4.955,  // 4.08  4.66  5.25  5.83
        hpRate: 4.955,   // 4.08  4.66  5.25  5.83
        criticalRate: 3.305,   //  2.72  3.11   3.5   3.89
        criticalStrike: 6.605,  //  5.44  6.22  6.99  7.77
        defRate: 6.195,   //  5.1  5.83  6.56   7.29
        charging: 5.505  //  4.53   5.18   5.83   6.48
    }
}


const CLIPDATA = CLIPDATA_TYPE.mid

const CONFIG = {
    chance: 20,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        criticalStrike: 62.2,
        dmgRate: 46.6 * 1,
        attRate: 46.6 * 1
    },
    clips: ["charging", "attRate", "criticalRate", "criticalStrike"],
    clips_initial: [
        ["charging", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "reaction", "criticalRate", "criticalStrike"],
        ["charging", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "reaction", "attRate", "criticalRate"]
    ],
    reactionK : 1.5,
    reactionDmgAdd: 0,
    buffs: [  
        { charging: 20}, // 充能圣遗物
        // { dmgRate: 15, criticalRate: 20}, // 冰套圣遗物
        // { criticalRate: 15 }, // 双冰
        { dmgRate: 18 }, // 天赋
        // { fixedAtt: 1202, attRate:20},  // 班尼
        // { jk:15, dmgRate: 30, dmgZhi: 4000},  // 申鹤
        { dmgRate: 40 , jk: 40}, // 万叶
        { attRate:48, dmgRate: 60,criticalRate: 15 }, // 莫娜
        // { attRate:48}, // 心海
        { attRate:20}, // 辅助宗室套
    ]
}

let charaters = {
    units : {
        name: "神里",
        reaction: 0,
        charging: 100,
        defRate: 0,
        defence: 784,
        hp:12858,
        base: 342,    
        criticalRate: 5,    
        criticalStrike: 88.4,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}

let cons = 4;
let select = 2
/* 神(n + 1) 猫 万(0+0) 莫(0+0)   4 + n
    0命神里 817002    4金
    2命神里 1121002   6金   37%
    3命神里 1323419   7金   62%
    4命神里 1556964   8金   90.5%
*/

/* 神(n + 1) 申(0+1) 万(0+0) 心(0+0)   6 + n
    0命神里 1038283     6金
    2命神里 1328166     8金
    3命神里 1521623     9金
    4命神里 1790144     10金
*/

/* 神(n + 1) 申(1+1) 万(0+0) 心(0+0)   7 + n
    0命神里 1182295     7金
    2命神里 1472178     9金
    3命神里 1665195     10金
    4命神里 1959053     11金
*/

/* 神(n + 1) 万(0+0) 班 莫(0+0)    充能套  4 + n
    0命神里 980061     4金   绿剑
    2命神里 1344735     6金  绿剑  37%
    3命神里 1587551     7金  绿剑  62%
    4命神里 1867707     8金  绿剑
*/

calc(calc_clips_initial());

function calc(normal) {
    let charactersDmg = [];
    let count = 0;
    Object.values(WEAPONS).forEach(weapon => {
        if(weapon.weapontype == "sword") {
            charactersDmg.push(getDMG([charaters.units,weapon,{fixedAtt:311,...CONFIG.clips_main},normal], (data,second) => {
                count++;
                data = objValueAdd([data, ...CONFIG.buffs]);
                data.hp = data.hp*(1 + (data.hpRate||0)/100) + 4780;
                if(weapon.skill) { weapon.skill(data); }
                let slipQadd = data.charging * 0.25
                if(slipQadd > 75) {
                    slipQadd = 75
                }
                data.qDmgAdd = (data.qDmgAdd || 0) +  slipQadd;
                let rea = { reaction:data.reaction, reactionQ:CONFIG.reactionK, reactionDmgAdd:CONFIG.reactionDmgAdd }
                // 2.304  3.9168
                let aDmg = getDmg({ data:{...data, dmgRate:data.dmgRate + 30 + 16}, rate: 0.9039 + 0.9624+1.2379+0.4477*3+1.5455}).qw * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let aZhongDmg = getDmg({ data:{...data, dmgRate:data.dmgRate + 30 + 60 + 298 + 16, dmgZhi:data.dmgZhi||0}, rate: 1.0897*3}).qw * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                // 10e 4.3056  13e 5.083   10s   e后6秒内普攻重击增伤30
                data.criticalRate += (data.qcriticalRate || 0)
                let EDmg = getDmg({ data, rate: 5.083}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let EDmgRea = getDmg({ data, rate: 5.083, rea}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                // 3.0321 / 2.0214       3.5796/2.3864
                let qRate = cons >= 3? [2.3864,3.5796] : [2.0214,3.0321]
                let QDmgQGDmgZhi = getDmg({ data:{...data, dmgRate:data.dmgRate + (data.qDmgAdd||0)+ (data.qDmg||0), criticalRate:data.criticalRate, dmgZhi:(data.dmgZhi||0)}, rate: 0}).qw * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, cons>=4? 30:0); //
                let QDmgQG = getDmg({ data:{...data, dmgRate:data.dmgRate + (data.qDmgAdd||0)+ (data.qDmg||0), criticalRate:data.criticalRate}, rate: qRate[0] * 20 * (cons >= 2? 1.4 : 1)}).qw * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, cons>=4? 30:0); //
                let QDmgZF = getDmg({ data:{...data, dmgRate:data.dmgRate + (data.qDmgAdd||0)+ (data.qDmg||0), criticalRate:data.criticalRate}, rate: qRate[1]}).qw * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, cons>=4? 30:0); //
    
                if(data.phyRate) { // 物理特效伤害
                    data.phyDmg = getDmg({data:{...data, dmgRate:0}, rate:data.phyRate}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);  // 
                }
    
                let point = aDmg * 0 + aZhongDmg * 0 + EDmg*0 + (QDmgQG + QDmgZF + QDmgQGDmgZhi * 12) 
                let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
                return {point, data, second, attack, msg:{aDmg, EDmg,EDmgRea, QDmgQG, QDmgZF, QDmgQGDmgZhi,aZhongDmg}}
            }))
        }
        
    })
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point)
    });
    console.log(charactersDmg[select]);
    console.log(charactersDmg.map((v, n)=>{
        return  `${n}得分: ${v.data.weaponname} : ${v.point.toFixed(0)}`
    }));
    console.log(count);
}

function getDMG(dataarr, getRet){
    let chance = CONFIG.chance;
    let _ret = [];
    for (let index = 0; index <= chance; index++) {
        let c1 = index; 
        for (let index2 = 0; index2 <= chance - index; index2++) {
            let c2 = index2;
            for (let index3 = 0; index3 <= chance-index-index2; index3++) {
                let c3 = index3;
                let c4 = chance - index - index2 - index3;
                let cs = [c1, c2, c3, c4];
                var tmpdata = {};
                CONFIG.clips.forEach((v, i) => {
                    tmpdata[v] = CLIPDATA[v] * cs[i]
                })
                _ret.push(tmpdata);
            }
        }
    }
    _ret = _ret.map(second => {
        let panel = objValueAdd([...dataarr, second]);
        return  getRet(panel, second);
    });
    _ret.sort((b,a)=> a.point-b.point);
    return _ret[0];
}

// 固定攻击力 19  防御力23 生命值299 5.3% 
function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        });
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
